using System;
using System.Threading.Tasks;
using JlPay.Sdk.Core;
using JlPay.Sdk.Services.Trans.Common;
using JlPay.Sdk.Models.Trans.Common;

namespace JlPay.Sdk.Examples.Trans.Common
{
    /// <summary>
    /// 交易 - Common预授权完成功能使用示例
    /// </summary>
    public class PreAuthCompleteDemo
    {
        /// <summary>
        /// 构建预授权完成请求
        /// </summary>
        /// <returns>请求对象</returns>
        private static PreAuthCompleteRequest BuildPreAuthCompleteRequest()
        {
            var request = new PreAuthCompleteRequest();
            
            // 设置请求参数
            request.MchId = "849584358120018";
            request.TermNo = "60131234";
            request.PayType = "wxpay";
            request.OutTradeNo = "ld201911023465529";
            request.OriOutTradeNo = "ld2019102216465525";
            request.OriTransactionId = "80011557201909171853011568";
            request.GuaranteeAuthCode = "TEST_AUTH_CODE";
            request.TotalFee = "100";
            request.Remark = "测试预授权完成";
            request.DeviceInfo = "test_device";
            request.MchCreateIp = "127.0.0.1";
            request.TransAddress = "北京市朝阳区";
            request.AreaCode = "110000";
            request.Longitude = "116.397128";
            request.Latitude = "39.916527";
            
            return request;
        }

        /// <summary>
        /// 预授权完成示例
        /// </summary>
        public static async Task PreAuthCompleteDemoAsync()
        {
            Console.WriteLine("开始执行预授权完成示例");

            // 使用DemoConfig创建配置对象
            var config = DemoConfig.CreateConfigWithCrypto();
            Console.WriteLine("配置初始化完成，已启用自动加密和解密功能");

            var preAuthCompleteService = new PreAuthCompleteService(config);

            // 创建预授权完成请求
            var request = BuildPreAuthCompleteRequest();

            try
            {
                // 发送请求
                var response = await preAuthCompleteService.PreAuthCompleteAsync(request);

                // 使用优化的响应处理
                response.HandleResponse("预授权完成", config);
            }
            catch (Exception ex)
            {
                // 使用优化的异常处理
                var response = new PreAuthCompleteResponse();
                response.HandleResponseWithException("预授权完成", config, ex);
            }

            Console.WriteLine("预授权完成示例执行完毕");
        }

        /// <summary>
        /// 主程序入口
        /// </summary>
        public static async Task Main(string[] args)
        {
            Console.WriteLine("=" + new string('=', 50));
            Console.WriteLine("===== 预授权完成示例 开始 =====");
            Console.WriteLine("=" + new string('=', 50));

            await PreAuthCompleteDemoAsync();

            Console.WriteLine("=" + new string('=', 50));
            Console.WriteLine("===== 预授权完成示例执行完毕 =====");
            Console.WriteLine("=" + new string('=', 50));
        }
    }
} 